using System;
using System.Collections.Generic;
using System.Text;

namespace PersistenceObject.Framework.Expressions
{
    public class Table
    {
        public Table(string name)
        {
            this.relation = null;
            this.name = name;
        }

        public Table(string name, Table inheritTable, FieldAdapter[] inheritField)
        {
            this.relation = null;
            this.name = name;
            this.inheritTable = inheritTable;
            this.inheritField = inheritField;
        }

        internal string GetTableName()
        {
            return this.name;
        }

        public RelationTable INNER(Table table, FieldAdapter field, FieldAdapter field1)
        {
            RelationTable relationTable = new RelationTable(this.name);
            relationTable.Relating = string.Concat(new string[] { " INNER JOIN ", table.name, " ON ", field.At(this).Name, "=", field1.At(table).Name });
            return relationTable;
        }

        public RelationTable LEFT(Table table, FieldAdapter field, FieldAdapter field1)
        {
            RelationTable relationTable = new RelationTable(this.name);
            relationTable.Relating = string.Concat(new string[] { " LEFT JOIN ", table.name, " ON ", field.At(this).Name, "=", field1.At(table).Name });
            return relationTable;
        }

        public RelationTable RIGHT(Table table, FieldAdapter field, FieldAdapter field1)
        {
            RelationTable relationTable = new RelationTable(this.name);
            relationTable.Relating = string.Concat(new string[] { " RIGHT JOIN ", table.name, " ON ", field.At(this).Name, "=", field1.At(table).Name });
            return relationTable;
        }

        public Table As(string name)
        {
            if (string.IsNullOrEmpty(this.Relating))
            {
                Table t = new Table(string.Format("{0} {1}", this.Name, name));
                t.alias = name;
                return t;
            }
            return this;
        }

        private Table inheritTable;
        public Table InheritTable
        {
            get { return inheritTable; }
            set { inheritTable = value; }
        }

        private FieldAdapter[] inheritField;
        public FieldAdapter[] InheritField
        {
            get { return inheritField; }
            set { inheritField = value; }
        }

        private string alias;
        public string Alias
        {
            get { return alias; }
            set { alias = value; }
        }

        protected string name;
        public string Name
        {
            get
            {
                if (string.IsNullOrEmpty(this.Relating))
                {
                    return this.name;
                }
                return (this.name + this.Relating);
            }
        }

        private string relation;
        internal string Relating
        {
            get
            {
                return this.relation;
            }
            set
            {
                this.relation = value;
            }
        } 
    }
}
